package com.neulogistics.core.substation.api.application.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neulogistics.core.substation.api.application.converter.SubstationPOVMConverter;
import com.neulogistics.core.substation.api.application.converter.SubstationQueryPOConverter;
import com.neulogistics.core.substation.api.application.converter.SubstationTreePOVMConverter;
import com.neulogistics.core.substation.api.application.viewmodel.SubstationTreeViewModel;
import com.neulogistics.core.substation.api.application.viewmodel.SubstationViewModel;
import com.neulogistics.core.substation.domain.model.aggregate.Substation;
import com.neulogistics.core.substation.infrastructure.persistence.converter.SubstationEntityPOConverter;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.SubstationPO;
import com.neulogistics.core.substation.infrastructure.persistence.orm.service.ISubstationPOService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubstationQueryService implements ISubstationQueryService {
    @Resource
    ISubstationPOService substationPOService;

    @Override
    public SubstationViewModel getById(Integer id) {
        SubstationPO substationPO = substationPOService.getById(id);
        SubstationViewModel substationViewModel = SubstationPOVMConverter.POToVM(substationPO);
        return substationViewModel;
    }

    @Override
    public Page<SubstationViewModel> page(SubstationQuery substationQuery) {
        // 取出条件
        SubstationPO substationPOQuery = SubstationQueryPOConverter.queryToPO(substationQuery);
        long current = substationQuery.getPageNum();
        long pageSize = substationQuery.getPageSize();

        // 查询
        QueryWrapper<SubstationPO> queryWrapper = new QueryWrapper<>(substationPOQuery);
        Page<SubstationPO> page = substationPOService.page(new Page<>(current, pageSize), queryWrapper);

        // 构造结果
        Page<SubstationViewModel> viewModelPage = new Page<>(current, pageSize);
        List<SubstationViewModel> viewModelList = page.getRecords().stream()
                .map(SubstationPOVMConverter::POToVM).collect(Collectors.toList());
        viewModelPage.setRecords(viewModelList);
        viewModelPage.setTotal(page.getTotal());
        return viewModelPage;
    }

    @Override
    public List<SubstationTreeViewModel> list() {
        List<SubstationPO> poList = substationPOService.list();
        List<SubstationTreeViewModel> viewModelList = poList
                .parallelStream()
                .map(SubstationTreePOVMConverter::POToVM)
                .collect(Collectors.toList());
        return viewModelList;
    }

    @Override
    public Substation getSubstationByWarehouseId(Integer warehouseId) {
        LambdaQueryWrapper<SubstationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubstationPO::getSubWarehouseId, warehouseId);

        SubstationPO substationPO = substationPOService.getOne(queryWrapper);
        Substation substation = SubstationEntityPOConverter.POToEntity(substationPO);
        return substation;
    }
}
